package com.wantup.springboot.boot.jms.mkw.rocketmq.qurickstart;

import com.wantup.springboot.boot.jms.mkw.rocketmq.qurickstart.constants.Const;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2018/11/19 17:14
 * <p>
 * nameserver 是 中间协调
 */
public class Producer {

    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //要指定一个 组名
        DefaultMQProducer producer = new DefaultMQProducer("test_quick_producer_name");
        //设置地址 IP地址
        producer.setNamesrvAddr(Const.NAME_SERVER_ADDR);
        producer.setVipChannelEnabled(false);
        //开启 之后就能发消息
        producer.start();


        for (int i = 0; i < 20; i++) {
//            //创建消息  如果没有创建主题 会默认帮助创建

            Message message = new Message("test_quick_topic", //主题，先往主题里面  投递 消息
                    "TagA",//标签 作用就是进行消息的过滤
                    "key-"+i,//用户自定义的 用作唯一的标识
                    ("HELLO RocketMQ WCC").getBytes()//消息内容实体是byte[]
            );
            //发消息
            SendResult sendResult = producer.send(message);
            System.out.println("消息发出  " + sendResult);

        }

        producer.shutdown();

    }

}
